package messages;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdateServlet extends HttpServlet 
{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		req.setCharacterEncoding("UTF-8");
		String strId = req.getParameter("id");
		int id = Integer.parseInt(strId);
		String password = req.getParameter("password");
		String message = req.getParameter("message");
		String delete_flag = req.getParameter("delete_flag");
//		String delete_flag = req.getParameter("delete_flag");
		
		StringBuffer msg = new StringBuffer();
		
		Message m = new Message();
		if(password == null || password != m.getPassword())
		{
			msg.append("パスワードが正しくありません");
		}
		
		try
		{
			Connection con = DBManager.getConnection();
			String sql = "update messages set message = ?, delete_flag = ? where id = ?";
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, message);
			ps.setString(2, delete_flag);
			ps.setInt(3, id);
			ps.executeUpdate();
			
			ps.close();
						
			resp.sendRedirect("ListServlet");
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		
	}
	
}
